package forumTest;

import common.TestUtils;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import java.io.IOException;

import static java.lang.Thread.sleep;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-11-14
 * Time: 15:01
 */
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class ForumLoginTest {
    private static WebDriver driver = null;
    @BeforeAll
    static void setUp() throws InterruptedException {
        // 初始化驱动对象
        driver = TestUtils.createDriver();
    }

    @AfterAll
    static void tearDown() {
        // 关闭驱动
        driver.quit();
    }

    // 登录成功（使用登录白名单，跳过验证码校验）
    @Order(1)
    @ParameterizedTest
    @CsvSource({"lee,123456,white","fish,123456,white"})
    void successedLogin(String userName,String password,String code) throws InterruptedException, IOException {
        // 观察登录页面是否正常访问
        driver.get("http://118.25.26.147:58080/sign-in.html");
        // 登录操作
        driver.findElement(By.cssSelector("#username")).sendKeys(userName);
        driver.findElement(By.cssSelector("#password")).sendKeys(password);
        driver.findElement(By.cssSelector("#check")).sendKeys(code);
        // 点击登录按钮
        driver.findElement(By.cssSelector("#submit")).click();
        // 阻塞观察是否跳转
        sleep(1000);
    }

    // 登录失败
    // 表单缺失
    @Order(2)
    @ParameterizedTest
    @CsvSource({"lee,123456"})
    void failedLogin1(String userName,String password) {
        driver.get("http://118.25.26.147:58080/sign-in.html");
        // 登录操作
        driver.findElement(By.cssSelector("#username")).sendKeys(userName);
        driver.findElement(By.cssSelector("#password")).sendKeys(password);
        // 点击登录按钮
        driver.findElement(By.cssSelector("#submit")).click();

        String text = driver.findElement(By.cssSelector("#signInForm > div:nth-child(3) > div")).getText();
        Assertions.assertEquals("验证码不能为空",text);

    }

    // 无效用户名
    @Order(3)
    @ParameterizedTest
    @CsvSource({"girl,123456,white"})
    void failedLogin2(String userName,String password,String code) {
        driver.get("http://118.25.26.147:58080/sign-in.html");
        // 登录操作
        driver.findElement(By.cssSelector("#username")).sendKeys(userName);
        driver.findElement(By.cssSelector("#password")).sendKeys(password);
        driver.findElement(By.cssSelector("#check")).sendKeys(code);
        // 点击登录按钮
        driver.findElement(By.cssSelector("#submit")).click();

        String text = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
        Assertions.assertTrue(text.contains("用户名或密码错误"));

    }
    // 错误密码
    @Order(4)
    @ParameterizedTest
    @CsvSource({"lee,1234567890,white"})
    void failedLogin3(String userName,String password,String code) {
        driver.get("http://118.25.26.147:58080/sign-in.html");
        // 登录操作
        driver.findElement(By.cssSelector("#username")).sendKeys(userName);
        driver.findElement(By.cssSelector("#password")).sendKeys(password);
        driver.findElement(By.cssSelector("#check")).sendKeys(code);
        // 点击登录按钮
        driver.findElement(By.cssSelector("#submit")).click();

        String text = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
        Assertions.assertTrue(text.contains("用户名或密码错误"));

    }
    // 错误验证码
    @Order(5)
    @ParameterizedTest
    @CsvSource({"aaaaa,123456,failed"})
    void failedLogin4(String userName,String password,String code) {
        driver.get("http://118.25.26.147:58080/sign-in.html");
        // 登录操作
        driver.findElement(By.cssSelector("#username")).sendKeys(userName);
        driver.findElement(By.cssSelector("#password")).sendKeys(password);
        driver.findElement(By.cssSelector("#check")).sendKeys(code);
        // 点击登录按钮
        driver.findElement(By.cssSelector("#submit")).click();

        String text = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
        Assertions.assertTrue(text.contains("验证码校验失败，请重试!"));

    }
}
